﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using K.EnterpriseLibary.Entity;
using K.Presentation;
namespace CreateDictionaryWeb.UserControl
{
    public partial class CharacterInfoControl : System.Web.UI.UserControl
    {
        public List<CharacterInfoProvenance> ListCharacterInfoProvenance
        {
            get 
            {
                return ViewState["listCharacterInfoProvenance"] as List<CharacterInfoProvenance>;
            }
            set
            {
                ViewState["listCharacterInfoProvenance"] = value;
            }
        }

        public Character Character
        {
            get
            {
                return ViewState["characterForPage"] as Character;
            }
            set
            {
                ViewState["characterForPage"] = value;
            }
        }

        public CharacterInfo CharacterInfo
        {
            get
            {
                CharacterInfo characterInfo = new CharacterInfo();
                characterInfo.Character = ViewState["characterForPage"] as Character;
                characterInfo.Meaning = tbCharacterInfo.Text;
                characterInfo.SoundMark = tbPinyin.Text;
                characterInfo.PartOfSpeech = tbPartOfSpeech.Text;
                return characterInfo;
            }
            set
            {
                tbCharacterInfo.Text = value.Meaning;
                tbPartOfSpeech.Text = value.PartOfSpeech;
                tbPinyin.Text = value.SoundMark;
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                this.ListCharacterInfoProvenance = new List<CharacterInfoProvenance>();
                this.ListCharacterInfoProvenance.Add(new CharacterInfoProvenance());
                this.rpCharacterInfoProvenacne.DataSource = this.ListCharacterInfoProvenance;
                this.rpCharacterInfoProvenacne.DataBind();
            }
        }

        public void GetCharacterInfoList()
        {
            
            this.ListCharacterInfoProvenance.Clear();
            foreach (RepeaterItem item in this.rpCharacterInfoProvenacne.Items)
            {
                //if (item.ItemIndex == j) continue;
                //读取控件中的值
                UserControl.CharacterInfoProvenanceControl control = item.Controls[0] as UserControl.CharacterInfoProvenanceControl;
                //保存值
                control.CharacterInfo = this.CharacterInfo;
                this.ListCharacterInfoProvenance.Add(control.CharacterInfoProvenance);

            }
        }

        private bool ModifyCharacterInfoProvenance(string modifyFlag,int modifyItem)
        {
            //循环当前RepeaterItem;并保存值到List
            //增加或删除
            //重新绑定
            
            GetCharacterInfoList();

            if (modifyFlag == "A")
            {
                //增加
                //增加一个空值
                this.ListCharacterInfoProvenance.Add(new CharacterInfoProvenance());
            }
            else
            {
                //删除
                //CharacterInfoProvenance characterInfoProvenance =
                this.ListCharacterInfoProvenance.Remove(this.ListCharacterInfoProvenance[modifyItem]);     
            }




            //重新绑定控件
            //绑定全部的，即增加了一个新的
            this.rpCharacterInfoProvenacne.DataSource = this.ListCharacterInfoProvenance;
            this.rpCharacterInfoProvenacne.DataBind();

            //重新绑定值
            //循环list，将保存的值重新赋给绑定的每个控件
            int i = 0;
            foreach (RepeaterItem item in this.rpCharacterInfoProvenacne.Items)
            {
                if (i == this.rpCharacterInfoProvenacne.Items.Count - 1 && modifyFlag == "A") //最后一个不绑定
                    break;
                (item.Controls[0] as UserControl.CharacterInfoProvenanceControl).CharacterInfoProvenance = this.ListCharacterInfoProvenance[i];
                i++;

            }
            
            return true;
        }

        protected void btAddProvenance_Click(object sender, EventArgs e)
        {
            ModifyCharacterInfoProvenance("A", -1);
        }

        protected void rpCharacterInfoProvenacne_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            ModifyCharacterInfoProvenance("D", e.Item.ItemIndex);
        }

    }
}